<template>
  <div class="user">
    <basic-container>
      <avue-crud
        :option="option"
        ref="crud"
        v-model="form"
        :page="page"
        :simple-page="true"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        :table-loading="listLoading"
        @row-update="update"
        @search-change="handleFilter"
        @refresh-change="handleRefreshChange"
        @search-reset="handleRefreshChange"
        :before-open="handleOpenBefore"
        :data="list"
      >
        <template slot="menu" slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-circle-check"
            @click="handleUpdate(scope.row, 1)"
            >通过
          </el-button>
          <el-button
            type="text"
            icon="el-icon-circle-close"
            @click="handleUpdate(scope.row, 0)"
            >拒绝
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchAuditList, domainAudit } from "@/api/admin/domain";
import { auditOption } from "@/const/crud/admin/domain";
import { mapGetters } from "vuex";

export default {
  name: "table_user",
  data() {
    return {
      option: auditOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false //是否倒序
      },
      list: [],
      listLoading: true,
      role: [],
      form: {},
    };
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchAuditList(
        Object.assign(
          {
            page: this.page.currentPage,
            size: this.page.pageSize
          },
          this.params
        )
      ).then(response => {
        this.list = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.listLoading = false;
      });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    handleFilter(param, done) {
      this.page.currentPage = 1;
      this.params = param
      this.getList(this.page, param);
      done();
    },
    handleRefreshChange() {
      this.getList(this.page);
    },
    handleOpenBefore(show, type) {
      show();
    },
    changeFlag(status, id) {
      let num;
      if (status === 0) {
        num = 1;
      } else {
        num = 0;
      }
      updateUser({ status: num, id: id }).then(res => {
        this.getList(this.page);
        this.$notify.success("操作成功");
      });
    },
    handleUpdate(row, type) {
      if(type === 1){
        this.$confirm(
          `域名${row.domain}已经完成托管`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        ).then(() => {
          domainAudit({
            id: row.id,
            type: type
          }).then(() => {
            this.getList(this.page);
            done();
            this.$notify.success("操作成功");
          })
        });
      }else{
        domainAudit({
          id: row.id,
          type: type
        }).then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("操作成功");
        })
      }
      
      
    },
    create(row, done, loading) {
      putObj(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("创建成功");
        })
        .catch(() => {
          loading();
        });
    },
    update(row, index, done, loading) {
      putObj(this.form)
        .then(() => {
          this.getList(this.page);
          done();
          this.$notify.success("操作成功");
        })
        .catch(() => {
          loading();
        });
    },
    resetPwd(row){
      this.$confirm(
        "此操作将重置(用户名:" + row.username + ")的密码为123456, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        resetPassword(row.id)
          .then(() => {
            this.getList(this.page);
            this.$notify.success("操作成功");
          })
          .catch(() => {
            this.$notify.error("操作失败");
          });
      });
    },
  }
};
</script>
